<template>
	<view class="content">
		<view class="text-area">
			<text class="title">uniapp推送测试demo</text>
      <view class="user-list-item-left">
        <view class="icon xiaoxi"></view>
        <view class="text">接受消息开通与关闭</view>
      </view>
      <view class="user-list-item-right">
        <switch class="switch-scale" color="#69B574" :checked="checked" @change="changePush"/>
      </view>
		</view>
	</view>
</template>


<script>
  let isAndorid, PushManager, context, Instance, GeTuiSdk; 
  import {
    setStorageSync,
    getStorageSync
  } from "@/libs/util";
  
	export default {
    
		data() {
			return {
				checked:true
			};
		},
    
    onLoad() {
      // #ifdef APP-PLUS
      this.igexinTool();
      // #endif
    },
    
    onShow() {
      this.checked = getStorageSync('isMessage') !== false ? true : false;
    },
    
    methods: {
      changePush(obj) {
        this.checked = obj.detail.value;
        //uniapp提供的API无效
        if (this.checked) {;
          // #ifdef APP-PLUS
          this.turnOnPush();
          // #endif
        } else {
          // #ifdef APP-PLUS
          this.turnOffPush();
          // #endif
        }
        setStorageSync('isMessage',this.checked);
      },
      
      igexinTool() {
        if(plus.os.name == 'Android') {  
          isAndorid = true;  
        } else {
          isAndorid = false;   
        }
        
        if(isAndorid) {  
          PushManager = plus.android.importClass("com.igexin.sdk.PushManager");  
          context = plus.android.runtimeMainActivity().getContext();  
          Instance = PushManager.getInstance();
          this.checked = Instance.isPushTurnedOn(context); 
          //console.log('android push',Instance.isPushTurnedOn(context));
        } else {  
          GeTuiSdk = plus.ios.importClass("GeTuiSdk");  
          this.checked = GeTuiSdk.status();
          //console.log('ios push',GeTuiSdk.status());
        }
        setStorageSync('isMessage',this.checked);
      },
      
      //开启推送
      turnOnPush() {  
        if(isAndorid) {  
          Instance.turnOnPush(context);  
        } else {  
          GeTuiSdk.setPushModeForOff(false);  
        }  
      },
          
      //关闭推送  
      turnOffPush() {  
        if(isAndorid) {  
          Instance.turnOffPush(context); 
        } else {  
          GeTuiSdk.setPushModeForOff(true);  
        }  
      } 
		
    }
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}



	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
